package com.morehair.controller.admin;

import com.morehair.bean.bo.admin.AdReq;
import com.morehair.bean.pojo.Ad;
import com.morehair.bean.vo.admin.BaseData;
import com.morehair.bean.vo.admin.BaseRespVo;
import com.morehair.service.admin.AdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: DLX
 * @Date: 2021/6/4 23:50
 * @Description:
 **/
@RestController
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    AdService adService;

    @RequestMapping("list")
    @RequiresPermissions("admin:ad:list")
    public BaseRespVo adList(AdReq baseReq) {// *

        BaseData<Ad> adBaseData = adService.query(baseReq);
        return BaseRespVo.ok(adBaseData);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:ad:create")
    public BaseRespVo adCreate(@RequestBody Ad ad) {

        Ad newAd= adService.create(ad);
        if (newAd == null) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(newAd);
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:ad:update")
    public BaseRespVo adUpdate(@RequestBody Ad ad) {
        Ad adUpdatedData = adService.adUpdate(ad);
        if (adUpdatedData == null) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(adUpdatedData);
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:ad:delete")
    public BaseRespVo adDelete(@RequestBody Ad ad) {
        int code = adService.adDelete(ad);
        if (code == 200) {
            return BaseRespVo.ok();
        }
        return  BaseRespVo.fail();
    }
}
